{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f41b1fc1",
   "metadata": {},
   "source": [
    "# 字典\n",
    "字典可存储的信息量几乎不受限制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "eaa7b874",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Nathan\n"
     ]
    }
   ],
   "source": [
    "## 定义\n",
    "dict = {'name':'Nathan'}\n",
    "print(dict['name'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "20c6c06e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "1\n",
      "3\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "## 使用\n",
    "## 创建\n",
    "dict = {}\n",
    "dict['ONE']  = 1\n",
    "dict['TWO']  = 2\n",
    "print(len(dict))\n",
    "print(dict['ONE'])\n",
    "\n",
    "\n",
    "### 修改字典的值\n",
    "dict['TWO']  = 3\n",
    "print(dict['TWO'])\n",
    "\n",
    "### 删除字典中的KV键值对\n",
    "del dict['TWO']\n",
    "print(len(dict))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "89ec27bf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12\n"
     ]
    }
   ],
   "source": [
    "## 使用GET访问字典中的值\n",
    "\n",
    "dict = {'x':12,'y':66,'z':97}\n",
    "print(dict.get('x'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "1f4adc64",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x:12\n",
      "y:66\n",
      "z:97\n",
      "x\n",
      "y\n",
      "z\n",
      "z\n",
      "y\n",
      "x\n",
      "12\n",
      "66\n",
      "97\n",
      "97\n",
      "66\n",
      "12\n"
     ]
    }
   ],
   "source": [
    "## 遍历字典\n",
    "### 遍历字典中的所有键值对\n",
    "dict = {'x': 12, 'y': 66, 'z': 97}\n",
    "for k, v in dict.items():\n",
    "    print(str(k) + \":\" + str(v))\n",
    "### 遍历字典中红的所有键\n",
    "for k in dict.keys():\n",
    "    print(k)\n",
    "### 按顺序遍历字典中的所有键\n",
    "for k in sorted(dict.keys(),reverse=True):\n",
    "    print(k)\n",
    "### 遍历字典中的所有值\n",
    "for v in dict.values():\n",
    "    print(v)\n",
    "\n",
    "### 遍历字典中的所有值去重版\n",
    "for v in set(dict.values()):\n",
    "    print(v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7d68c440",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'x': 12, 'y': 66, 'z': 97}\n",
      "{'a': 14, 'b': 36, 'c': 37}\n",
      "{'i': 17, 'j': 16, 'k': 57}\n",
      "{'e': 'E', 'd': 'D', 'g': 'G'}\n",
      "{'e': 'E', 'd': 'D', 'g': 'G'}\n",
      "{'e': 'E', 'd': 'D', 'g': 'G'}\n",
      "{'e': 'E', 'd': 'D', 'g': 'G'}\n",
      "{'e': 'E', 'd': 'D', 'g': 'G'}\n"
     ]
    }
   ],
   "source": [
    "## 混合使用\n",
    "### 列表和字典的混合使用\n",
    "dict1 = {'x': 12, 'y': 66, 'z': 97}\n",
    "dict2 = {'a': 14, 'b': 36, 'c': 37}\n",
    "dict3 = {'i': 17, 'j': 16, 'k': 57}\n",
    "\n",
    "list_dict = [dict1,dict2,dict3]\n",
    "\n",
    "for lst in list_dict:\n",
    "    print(lst)\n",
    "\n",
    "\n",
    "list= []\n",
    "\n",
    "for lst in range(50):\n",
    "    new_list = {'e':'E','d':'D','g':'G'}\n",
    "    list.append(new_list)\n",
    "for lst in list[:5]:\n",
    "    print(lst)\n",
    "\n",
    "\n",
    "\n",
    "### 在字典中存储列表\n",
    "\n",
    "### 在字典中存储字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5524a267",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7231b56f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
